// 初始化一级类目
initClass(0, {
    "categoryLevel": 1
});

// 初始化三级类目
function initClass(level, data) {
    $.ajax({
        url: root.goodsInter.categoryByParam,
        cache: false, //禁用缓存
        contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
        type: "POST",
        data: data,
        async: false,
        success: function (data) {
            if (data != null && data.data.length > 0) {
                // 初始化一级类目
                if (level == 0) {
                    for (var i = 0; i < data.data.length; i++) {
                        $("#oneCategoryId").append("<option value='" + data.data[i].category_id + "'>" + data.data[i].category_name + "</option>");
                    }
                } else if (level == 1) {
                    for (var i = 0; i < data.data.length; i++) {
                        $("#twoCategoryId").append("<option value='" + data.data[i].category_id + "'>" + data.data[i].category_name + "</option>");
                    }
                } else if (level == 2) {
                    for (var i = 0; i < data.data.length; i++) {
                        $("#threeCategoryId").append("<option value='" + data.data[i].category_id + "'>" + data.data[i].category_name + "</option>");
                    }
                } else if (level == 3) {
                    for (var i = 0; i < data.data.length; i++) {
                        $("#fourCategoryId").append("<option value='" + data.data[i].category_id + "'>" + data.data[i].category_name + "</option>");
                    }
                }
            }
        },
        error: function () {
            console.log("!!!!获取商品类目异常-商品后台!!!!");
            layer.msg('获取商品类目异常-商品后台', {
                icon: 2,
                time: 3000
            });
        }
    });
}

$("#oneCategoryId").change(function () {
    changeClass(0);
});
$("#twoCategoryId").change(function () {
    changeClass(1);
});
$("#threeCategoryId").change(function () {
    changeClass(2);
});
$("#threeCategoryId").change(function () {
    changeClass(3);
});

// 类目选择变动函数
function changeClass(level) {
    // 一级类目点击事件,重新加载二级类目,清空三级类目内容
    if (level == 0) {
        $("#twoCategoryId").empty();
        $("#twoCategoryId").append("<option value=''>请选择二级品类</option>");
        $("#threeCategoryId").empty();
        $("#threeCategoryId").append("<option value=''>请选择三级品类</option>");
        $("#fourCategoryId").empty();
        $("#fourCategoryId").append("<option value=''>请选择四级品类</option>");
        if ($("#oneCategoryId").val() != "") {
            initClass(1, {
                "parentCateId": $("#oneCategoryId").val()
            })
        }
    }
    // 二级类目点击事件,重新加载三级类目
    if (level == 1) {
        $("#threeCategoryId").empty();
        $("#threeCategoryId").append("<option value=''>请选择三级品类</option>");
        $("#fourCategoryId").empty();
        $("#fourCategoryId").append("<option value=''>请选择四级品类</option>");
        if ($("#twoCategoryId").val() != "") {
            initClass(2, {
                "parentCateId": $("#twoCategoryId").val()
            })
        }
    }
    // 三级类目点击事件,重新加载三级类目
    if (level == 2) {
        $("#fourCategoryId").empty();
        $("#fourCategoryId").append("<option value=''>请选择四级品类</option>");
        if ($("#twoCategoryId").val() != "") {
            initClass(3, {
                "parentCateId": $("#threeCategoryId").val()
            })
        }
    }
}

// 初始化等级
initLevel();

function initLevel() {
    $.ajax({
        url: root.member.getLevelsByPage,
        cache: false, //禁用缓存
        contentType: 'application/x-www-form-urlencoded',
        type: "post",
        async: false,
        data: {pageNum: 1, pageSize: 100},
        success: function (data) {
            if (data != null && data.data.result.length > 0) {
                // 初始化一级类目
                for (var i = 0; i < data.data.result.length; i++) {
                    $("#gradeId").append('<option value="' + data.data.result[i].gradeId + '">' + data.data.result[i].gradeName + '</option>');
                }
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            console.log(XMLHttpRequest);
            console.log(textStatus);
            console.log(errorThrown);
            console.log("!!!!会员等级系统异常!!!!");
            layer.msg('会员等级系统异常!', {
                icon: 2,
                time: 3000
            });
            layer.closeAll();
        }
    });
}
var pointId =sessionStorage.getItem("pointId")
// 修改回显
if (pointId > 0) {
    $("#integralId").val(pointId);
    $.ajax({
        url: root.member.getPointById,
        cache: false, //禁用缓存
        dataType: "json",
        contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
        type: "post",
        data: {rulerId: pointId},
        success: function (data) {
            if (data.errorCode == 0) {
                if (data.data.oneCategoryId != null && data.data.oneCategoryId > 0) {
                    $("#oneCategoryId").val(data.data.oneCategoryId);
                    initClass(1, {
                        "parentCateId": data.data.oneCategoryId
                    })
                }
                if (data.data.twoCategoryId != null && data.data.twoCategoryId > 0) {
                    $("#twoCategoryId").val(data.data.twoCategoryId);
                    initClass(2, {
                        "parentCateId": data.data.twoCategoryId
                    })
                }
                if (data.data.threeCategoryId != null && data.data.threeCategoryId > 0) {
                    $("#threeCategoryId").val(data.data.threeCategoryId);
                    initClass(3, {
                        "parentCateId": data.data.threeCategoryId
                    })
                }
                if (data.data.fourCategoryId != null && data.data.fourCategoryId > 0) {
                    $("#fourCategoryId").val(data.data.fourCategoryId);
                }
                $("#gradeId").val(data.data.gradeId);
                $("#integralCoefficient").val(data.data.integralCoefficient);
                $("#state").val(data.data.state);
            } else {
                layer.msg(data.msg, {
                    icon: 2,
                    time: 3000
                });
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            console.log(XMLHttpRequest);
            console.log(textStatus);
            console.log(errorThrown);
            console.log("!!!!会员等级系统异常!!!!");
            layer.msg('会员等级系统异常!', {
                icon: 2,
                time: 3000
            });
            layer.closeAll();
        }
    });
}


// 表单提交
$(function () {
    $("#form-point").validate({
        rules: {
            oneCategoryId: "requiredCategory",
            gradeId: "requiredCategory",
        },
        submitHandler: function (from) {
            if($("#oneCategoryId").val()==""){
                layer.msg("请选择品类", {
                    icon: 2,
                    time: 3000
                });
                return false;
            }else if($("#twoCategoryId").val()=="") {
                layer.msg("请选择二级品类", {
                    icon: 2,
                    time: 3000
                });
                return false;
            }else if($("#threeCategoryId").val()=="") {
                layer.msg("请选择三级品类", {
                    icon: 2,
                    time: 3000
                });
                return false;
            }else if($("#fourCategoryId").val()=="") {
                layer.msg("请选择四级品类", {
                    icon: 2,
                    time: 3000
                });
                return false;
            }
            $.ajax({
                url: pointId > 0 ? root.member.updatePoint : root.member.savePoint,
                cache: false, //禁用缓存
                contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
                type: "POST",
                data: {
                    oneCategoryId: $("#oneCategoryId").val(),
                    oneCategoryName: $("#oneCategoryId").val() == "" ? "" : $("#oneCategoryId").find("option:selected").text(),
                    twoCategoryId: $("#twoCategoryId").val(),
                    twoCategoryName: $("#twoCategoryId").val() == "" ? "" : $("#twoCategoryId").find("option:selected").text(),
                    threeCategoryId: $("#threeCategoryId").val(),
                    threeCategoryName: $("#threeCategoryId").val() == "" ? "" : $("#threeCategoryId").find("option:selected").text(),
                    fourCategoryId: $("#fourCategoryId").val(),
                    fourCategoryName: $("#fourCategoryId").val() == "" ? "" : $("#fourCategoryId").find("option:selected").text(),
                    integralCoefficient: $("#integralCoefficient").val(),
                    rulerId: pointId,
                    state:$("#state").val()
                },
                success: function (data) {
                    if (data.errorCode == 0) {
                        layer.msg("保存成功", {
                            icon: 6,
                            time: 3000
                        });
                        setTimeout(function () {
                            parent.initTable11();
                            removeThisIframe();
                        })


                    } else {
                        layer.msg(data.msg, {
                            icon: 2,
                            time: 3000
                        });
                    }
                },
                error: function () {
                    console.log("!!!!会员等级系统异常!!!!");
                    layer.msg('会员等级系统异常', {
                        icon: 2,
                        time: 3000
                    });
                }
            });
        }
    });
});


$.validator.addMethod("requiredCategory", function (value, element) {
    if ($("#oneCategoryId").val() == "" && $("#gradeId").val() == "") {
        return false;
    }
    return true;
}, "请至少选择一项，定义积分系数！");

// 折扣限制输入
function reduce(obj) {
    obj.value = obj.value.replace(/\b(0+)/gi, ""); // 替换开头的0
    obj.value = obj.value.replace(/[^\d.]/g, ""); //清除"数字"和"."以外的字符
    obj.value = obj.value.replace(/^\./g, ""); //验证第一个字符是数字
    obj.value = obj.value.replace(/\.{2,}/g, "."); //只保留第一个, 清除多余的
    obj.value = obj.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
    obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3'); //只能输入两个小数
    if (obj.value != "" && obj.value < 1) {
        obj.value = 1;
    }
}

/*关闭详情页*/
function removeThisIframe() {
    var index = parent.layer.getFrameIndex(window.name);
    parent.layer.close(index);
}

function reduce22() {
    if($("#integralCoefficient").val() >3){
        $("#integralCoefficient").val("")
    }
}